$(function(){
    
        var currentCity=SYS_GLOBAL_CONFIG.getCurrentCity("currentCity");//
        var pdTypeId=parseInt(SYS_GLOBAL_CONFIG.getQueryString("pdTypeId"));//
        console.log(currentCity+"=====currentCity");
        var url=SYS_GLOBAL_CONFIG.reqUrl,
        async="true",
        type="post";
        var address=currentCity;//获取当前城市
        var currentPage=1;//默认设置当前页为第一页
        var current_Page=1;
        var totalPage=1;//设置默认总页数
        var show_count=3;//设置默认每次加载的条数
        var dataProductNext="";
        var productTypeId=0;
        var requestData3="";
        var requestData=JSON.stringify({
            "code":"getPdOrServTypeList",
            "version":"1.0",
            "data":{
                "parentServiceId":0,
                "type":"PRODUCT",
                "parentPdTypeId":pdTypeId
            }
        });
        var requestData2=JSON.stringify(
            {
                "code":"getProductList",
                "version":"1.0",
                "data":{
                        "areaId":"",
                        "parentPdTypeId":pdTypeId,
                        "userId":0,
                        "address":currentCity,
                        "currentPage":current_Page,
                        "showCount":show_count,
                        "productName":""
                }
            });
              
        getData(url,requestData,handdle_func1,async,type);
        getData(url,requestData2,handdle_func2,async,type); 
    
        function getData(url,datas,call_func,async,type){
            console.log("qingqiucanshu:"+datas);
            $.ajax({
                url:url,

                type:type,

                datatype:"json",
                
                async:async,

                contentType:"application/json",

                data:datas,

                success:function(data){
                    call_func(data);

                },
                error:function(){
                    console.log("失败-----------");
                }   
            });        
        }
        
        function handdle_func1(data){
            console.log(data);  
            var list=data.data.productTypes;
            console.log(data.data.productTypes)
            var listLen=list.length;
            var li='<li class="zwl-li cl333 bg36c8b8" data-productTypeId="">全部</lid>';
            for(var i=0;i<listLen;i++){
                li+='<li class="zwl-li cl333" data-productTypeId='+data.data.productTypes[i].pdTypeId+'>'+data.data.productTypes[i].pdTypeName+'</li>';
            }
            console.log(li);
            $(".zwl-ul").html(li);
            $(".zwl-li").on("click",function(){
                $("#goods-lists").html("");
                $(this).addClass("bg36c8b8");
                $(this).siblings().removeClass("bg36c8b8");
                productTypeId=parseInt($(this).attr("data-productTypeId"));
               requestData3=JSON.stringify({
                   "code":"getProductList",
                   "version":"1.0",
                   "data":{
                       "areaId":"",
                        "parentPdTypeId":pdTypeId,
                        "userId":0,
                        "address":currentCity,
                        "currentPage":current_Page,
                        "showCount":show_count,
                        "productName":"",
                        "productTypeId":productTypeId
                    }
               });  
            getData(url,requestData3,handdle_func2,async,type); 
                current_Page=1;
            });
        }  
        
    function handdle_func2(data){
        var datas=data.data.datas;
        var lens=datas.length;
        totalPage=data.data.totalPage;//总页数
        var dataStr1="";
        var dataStr2="";
        var arr1=[];
        var arr2=[];
        if(lens>0){
           for(var i=0;i<lens;i++){
                if(datas[i].sellerFees.length>0){
                    if(datas[i].sellerFees[0].isAvaliable==1){
                        arr2.push(datas[i]);    
                    }else{
                        arr1.push(datas[i]);
                    }
                }else{
                    arr1.push(datas[i]);
                }
           }
        }
        if(arr1.length>0){
            for(var i=0,len=arr1.length;i<len;i++){
               var isauth="";
               if(arr1[i].seller.isSellerAuth==0){
                   isauth='<span>(未认证)</span>';
               }else{
                   isauth='<img src="./images/vip_icon.png" alt="">';
               }
               dataStr1+='<div data-proId="'+arr1[i].productId+'" class="zwl-noCertificate"><div class="goodNoCertificate"><img class="zwl-img" src="'+arr1[i].mainImgPath+'" alt=""><div class="zwl-shoppingDetail"><p class="zwl-p">'+arr1[i].productName+'</p><span class="shop1">'+arr1[i].productTypeName+'</span><span class="shopRe1">'+isauth+'</span><span class="span2"> <span class="money">'+arr1[i].saleprice+'元/</span><span class="cess">'+arr1[i].unit+'</span></span></div></div></div>';

           }
       }
        var downS='';
        if(arr2.length>0){
            for(var i=0,len=arr2.length;i<len;i++){
               var isauth="";
               if(arr2[i].seller.isSellerAuth==0){
                   isauth='<span>(未认证)</span>';
               }else{
                   isauth='<img src="./images/vip_icon.png" alt="">';
               }
                var main_img='';
                var k_arr=arr2[i].productImgs;
                var k_len=k_arr.length;
                if(k_len>0){
                    for(var k=0;k<k_len;k++){
                        if(k===3){
                            break;    
                        }
                        main_img+='<img src="'+k_arr[k].FULLPATH+'" alt="">';    
                    }    
                }
                var t_len=arr2[i].tagServices.length;
                var arrTag=arr2[i].tagServices;
                if(t_len>2){
                    downS='<i class="down"></i>';
                }
                var tags='';
                var tagStr='';
                for(var t=0;t<t_len;t++){
                    tagStr+='<p class="tags_info"><img src="'+arrTag[t].tagIconPath+'" alt=""><span>'+arrTag[t].userRemark+'</span></p>';
                    if(t<3){
                        tags+='<span>'+arrTag[t].name+'</span>'; 
                    }
                }
                dataStr2+='<div class="zwl-Certificate"><div class="zwl-show" data-proId="'+arr2[i].productId+'"><div class="goodNoCertificate"><img class="zwl-img" src="'+arr2[i].mainImgPath+'" alt=""><div class="zwl-shoppingDetail"><p class="zwl-p">'+arr2[i].productName+'</p><p class="zwl-way">'+tags+'</p><span class="shop">'+arr2[i].productTypeName+'</span><span class="shopRe">'+isauth+'</span><span class="span1"><span class="money">'+arr2[i].saleprice+'元/</span><span class="cess">'+arr2[i].unit+'</span></span></div></div><div class="zwl-pic">'+main_img+'<img src="'+arr2[i].mainImgPath+'" alt=""></div></div><div class="zwl-benefit max2_h">'+tagStr+downS+'</div></div>';
           }
       }
        $("#goods-lists").append(dataStr1+""+dataStr2);
        var down_flag=1;
        if(downS!=''){
            $(".zwl-benefit i").off("click");
            $(".zwl-benefit i").on("click",function(){
                if(down_flag==1){  
                    $(this).addClass("up").removeClass("down").parent(".zwl-benefit").addClass("auto_h").removeClass("max2_h");
                    down_flag=0;
                }else{
                    $(this).addClass("down").removeClass("up").parent(".zwl-benefit").addClass("max2_h").removeClass("auto_h");
                    down_flag=1;
                }
            });    
        }
        $(".zwl-noCertificate,.zwl-show").on("click",function(){
            var proId=$(this).attr("data-proId");
            location.href="./goodDetail.html?productId="+proId;
        }); 
    }
    /**
     *滚动加载更多*
     **/
   $(window).scroll(function(){
        totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop())-400;
        $(".data-loading").removeClass("hide").addClass("show");
        if((parseFloat($(document).height())-400)==totalheight){ 
            console.log(totalPage+"---"+current_Page);
            if(totalPage>current_Page){
                current_Page+=1;
                var data_p=JSON.stringify({  
                    "code":"getProductList",
                    "version":"1.0",
                    "data":{
                        "areaId":"",
                        "parentPdTypeId":pdTypeId,
                        "userId":0,
                        "address":currentCity,
                        "currentPage":current_Page,
                        "showCount":show_count,
                        "productName":""
                    }
                });
                getData(url,data_p,handdle_func2,async,type);    
            }else{
                $(".data-loading").html("已加载全部");
            }
        } 
     });
   
});